package com.baidu.service.impl;

import com.baidu.utils.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baidu.vo.PageVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baidu.pojo.TbContent;
import com.baidu.service.TbContentService;
import com.baidu.mapper.TbContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class TbContentServiceImpl extends ServiceImpl<TbContentMapper, TbContent>

    implements TbContentService{


    @Autowired
    TbContentMapper tbContentMapper;

    public ResultResponse updContent(TbContent content){
        UpdateWrapper<TbContent> tbContentUpdateWrapper = new UpdateWrapper<>();
        tbContentUpdateWrapper.eq("id",content.getId());
        tbContentMapper.update(content,tbContentUpdateWrapper);
        return ResultResponse.SUCCESS();
    }


    @Override
    public ResultResponse getContentList(PageVo vo) {
        Page<TbContent> page = new Page<>(vo.getCurrent(),vo.getSize());
        QueryWrapper<TbContent> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort_order");
        Page<TbContent> contentPage = tbContentMapper.selectPage(page,wrapper);
        List<TbContent> records = contentPage.getRecords();
        return ResultResponse.SUCCESS(contentPage);
    }

    @Override
    public ResultResponse addContentList(TbContent content) {
        int insert = tbContentMapper.insert(content);
        return ResultResponse.SUCCESS(insert);
    }

    @Override
    public ResultResponse delContentByIds(Integer[] ids) {
        List<Integer> list = new ArrayList<>();
        for (Integer id : ids) {
            list.add(id);
        }
        int i = tbContentMapper.deleteBatchIds(list);
        return ResultResponse.SUCCESS(i);
    }

    @Override
    public ResultResponse updateStateYes(String ids) {
        Integer update = tbContentMapper.updateStateYes(ids);
        return ResultResponse.SUCCESS(update);
    }

    @Override
    public ResultResponse updateStateNo(String ids) {
        Integer update = tbContentMapper.updateStateNo(ids);
        return ResultResponse.SUCCESS(update);
    }

    @Override
    public ResultResponse updateContent(TbContent content) {
        int i = tbContentMapper.updateById(content);
        return ResultResponse.SUCCESS(i);
    }

}




